package portal.mybatis.dao.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import portal.beans.Gpu;
import portal.beans.Node;
import portal.common.MesageCode;
import portal.mybatis.dao.mapper.HostMapper;
@Service
public class HostService {
	@Autowired
	HostMapper hostMapper;
	@Autowired
	GpuService gpuService;
	public boolean isNodeExist(String nodeName){		
		if(null==selectByName(nodeName))
			return false;
		return true;
	}
	public boolean isIpExist(String ip){
		if(null==selectByIp(ip))
			return false;
		return true;
	}
	public int updateNodeById(Node node){
		if(node==null ||node.getNodeId()<0)
			return 0;
		return hostMapper.updateNodeById(node);
		//return 1;
	}
	public int updateNodeById(int id,String groupName,String ip,String gpuKey){
		return updateNodeById(id,groupName,ip,gpuKey,null);
		/*Node node =new Node();
		node.setNodeId(id);
		node.setGroupName(groupName);
		if(ip==null || ip.length()<=4)
			return -1;	
		node.setNodeIp(ip);
		Gpu g=gpuService.selectByName(gpuKey);
		if(g!=null)
			node.setGpuId(g.getGpuId());
		node.setInactive(MesageCode.FALSE);//默认更新为有效节点
		//System.out.println("isActive:0000000000000000   "+ node.getInactive() );
		return updateNodeById(node);*/
	}
	public int updateNodeById(int id,String groupName,String ip,String gpuKey,String status){
		Node node =new Node();
		node.setNodeId(id);
		node.setGroupName(groupName);
		if(ip==null || ip.length()<=4)
			return -1;	
		node.setNodeIp(ip);
		node.setStatus(status);
		Gpu g=gpuService.selectByName(gpuKey);
		if(g!=null)
			node.setGpuId(g.getGpuId());
		node.setInactive(MesageCode.FALSE);//默认更新为有效节点
		//System.out.println("isActive:0000000000000000   "+ node.getInactive() );
		return updateNodeById(node);
	}
	
	public int insertNode(String nodeName,String groupName,String ip,String gpuKey){
		/*Node node =new Node();
		node.setNodeName(nodeName);
		if(ip==null || ip.length()<=4)
			return -1;			
		node.setNodeIp(ip);
		node.setGroupName(groupName);
		Gpu g=gpuService.selectByName(gpuKey);
		if(g!=null)
			node.setGpuId(g.getGpuId());
		node.setInactive(MesageCode.FALSE);
		//System.out.println("isInactive : " + node.getInactive());
		return insertNode(node);*/
		return insertNode(nodeName,groupName,ip,gpuKey,null);
	}
	public int insertNode(String nodeName,String groupName,String ip,String gpuKey,String status){
		Node node =new Node();
		node.setNodeName(nodeName);
		if(ip==null || ip.length()<=4)
			return -1;			
		node.setNodeIp(ip);
		node.setGroupName(groupName);
		Gpu g=gpuService.selectByName(gpuKey);
		if(g!=null)
			node.setGpuId(g.getGpuId());
		node.setStatus(status);
		node.setInactive(MesageCode.FALSE);
		//System.out.println("isInactive : " + node.getInactive());
		return insertNode(node);
	}
	public int insertNode(Node node){
		if(node==null)return -1;
		Node find=selectByName(node.getNodeName());
		if(find!=null){
			node.setNodeId(find.getNodeId());
			if(find.getInactive()==MesageCode.TRUE)
				node.setInactive(MesageCode.FALSE);
			return updateNodeById(node);
		}
		return hostMapper.insertNode(node);
	}
	public int deleteById(Node node){
		if(node==null)return -1;
		return hostMapper.deleteById(node);
	}
	public int deleteByNameOrIdOrIp(Node node){
		if(node==null)return -1;
		return hostMapper.deleteByNameOrIpOrId(node);
	}
	public long countAll(){
		return hostMapper.countAll();
		//return 0L;
	}
	public Node selectByName(String nodeName){
		if(nodeName==null)return null;
		List<Node>list =hostMapper.selectByName(nodeName);
		if(list==null || list.size()==0)
			return null;
		return list.get(0);
	}
	public Node selectByIp(String ip){
		if(ip==null)return null;
		List<Node>list =hostMapper.selectByIp(ip);
		if(list==null || list.size()==0)
			return null;
		return list.get(0);
	}
	public int updateAllNodeToInactive(){
		return hostMapper.updateNodeToInactive();
	}
	public List<Node> selectAll(){
		return hostMapper.selectAll();
	}
	public int updateNodeToActive(String name){
		return updateNodeInactiveStatus(name,MesageCode.FALSE);
	}
	public int updateNodeToInactive(String name){
		return updateNodeInactiveStatus(name,MesageCode.TRUE);
	}
	public int updateNodeInactiveStatus(String name,int status){
		Node node =selectByName(name);
		if(node==null)
			return -1;
		Node update =new Node();
		update.setNodeId(node.getNodeId());
		update.setInactive(status);
		return this.updateNodeById(update);
	}
}
